home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / tc_tsr10.zip / PRTSC.C < prev    next >
Text File  |  1991-06-18  |  15KB  |  371 lines

  1. /*--------------------------------------------------------------------------*
  2.  |   PrtSc.C                                                                |
  3.  |     contains screen, printer, keyboard, and disk I/O routines            |
  4.  |   PrtSC.c is supplied as is with no warranty, expressed or implied.      |
  5.  |   It is not copyrighted, either.                                         |
  6.  *--------------------------------------------------------------------------*/
  7.  
  8. /*--------------------------------------------------------------------------*
  9.  | Author:                                                                  |
  10.  | Sherif El-Kassas        .       :.                                       |
  11.  | EB dept           \_____o__/ __________                                  |
  12.  | Eindhoven U of Tec       .. /                                            |
  13.  | The Netherlands            /             Email: elkassas@eb.ele.tue.nl   |
  14.  *--------------------------------------------------------------------------*/
  15.  
  16. /*--------------------------------------------------------------------------*
  17.  | The author shall not be liable to the user for any direct, indirect      |
  18.  | or consequential loss arising from the use of, or inability to use,      |
  19.  | any program or file howsoever caused.  No warranty is given that the     |
  20.  | programs will work under all circumstances.                              |
  21.  *--------------------------------------------------------------------------*/
  22.  
  23. #include <dos.h>
  24. #include <bios.h>
  25. #include <fcntl.h>
  26. #include <stat.h>
  27. #include <io.h>
  28. #include <stdlib.h>
  29. #include "prtsc.h"
  30.  
  31. /*------------------------------------------------------------------------*
  32.  |                           GLOBAL VARIABLES                             |
  33.  *------------------------------------------------------------------------*/
  34.  
  35. unsigned screen_pos = 0;  /* pointer to the current screen location         */
  36. char far *screen    = (char far *) 0xB8000000L; /* pointer to video RAM     */
  37.                           /* assume (for now) we have a colored monitor !   */
  38.  
  39. char far * save_buffer;   /* pointer to a buffer that is used to save       */
  40.                           /* box borders. (save_buffer is declared as a     */
  41.                           /* far pointer to make sure that it's compatible  */
  42.                           /* with the screen pointer                        */
  43.  
  44. char box_buffer[((MAX_X+1)*2+(MAX_Y+1)*2)*2]; /* reserve the maximum amount */
  45.                                               /* of memory needed to save   */
  46.                                               /* the rubber-band box        */
  47.  
  48. byte old_cursor_color;                        /* to save cursor location    */
  49. int  old_cursor_pos;                          /* and attribute              */
  50.  
  51. /*------------------------------------------------------------------------*
  52.  |                  Initialize screen and buffer pointers                 |
  53.  *------------------------------------------------------------------------*/
  54. void  initialize_video(void){
  55.      union REGS reg;
  56.  
  57.      reg.h.ah = 0x0F;         /* interrupt 0x10 function 0x0F get current   */
  58.      int86(0x10, ®, ®); /* display mode. if al==7 i.e. mono screen    */
  59.      if (reg.h.al == 7)       /* change the value of the screen pointer     */
  60.        screen = (char far *) 0xB0000000L;
  61.      save_buffer = MK_FP(_DS, (unsigned) box_buffer); /* point to box buffer*/
  62.      old_cursor_pos   = 0;
  63.      old_cursor_color = screen[1];
  64.  
  65. }/* initialize_video() */
  66.  
  67. /*------------------------------------------------------------------------*
  68.  |       Read a key using BIOS function (interrupt 0x16 function 0)       |
  69.  *------------------------------------------------------------------------*/
  70. int  getkey(void){
  71.      register  int  c;
  72.      c = bioskey(0);
  73.      return( (c&0x00ff) ? (c & 0x00ff) : c );
  74. } /* getkey() */
  75.  
  76. /*------------------------------------------------------------------------*
  77.  |                  Move inverse video cursor to col and row.             |
  78.  *------------------------------------------------------------------------*/
  79. void move_cursor( byte col, byte row ){
  80.      screen[old_cursor_pos+1] = old_cursor_color; /* restore old color      */
  81.      old_cursor_pos = row*160+col*2;              /* save cursor position   */
  82.      old_cursor_color = screen[old_cursor_pos+1]; /* save color             */
  83. /*     screen[old_cursor_pos+1] = CURSOR_COLOR; /* put cursor at col and row  */
  84.     if(screen[old_cursor_pos+1]==CURSOR_COLOR)
  85.          screen[old_cursor_pos+1] = CURSOR_COLOR1;
  86.     else screen[old_cursor_pos+1] = CURSOR_COLOR;
  87. }/* move_cursor */
  88.  
  89. /*------------------------------------------------------------------------*
  90.  |           Make col and row the current screen position                 |
  91.  *------------------------------------------------------------------------*/
  92. void gotoxy( byte col, byte row ){
  93.      screen_pos = row*((MAX_X+1)*2)+col*2;
  94. }/* gotoxy */
  95.  
  96. /*------------------------------------------------------------------------*
  97.  |   Write character and attribute at current location and move pointer   |
  98.  *------------------------------------------------------------------------*/
  99. void putc(char c, byte color){
  100.      screen[screen_pos] = c;    /* write character                        */
  101.      screen_pos++;              /* point to color location                */
  102.      screen[screen_pos] = color;/* write color                            */
  103.      screen_pos++;              /* point to next position                 */
  104. }/* putc */
  105.  
  106. /*------------------------------------------------------------------------*
  107.  |         Write character and attribute "num" times                      |
  108.  *------------------------------------------------------------------------*/
  109. void repeat_char(char c, register int num, byte color){
  110.      register  int i;
  111.  
  112.      for(i=0;i<num;i++) putc(c, color);
  113. }/* repeat_char */
  114.  
  115. /*------------------------------------------------------------------------*
  116.  |                        Write string                                    |
  117.  *------------------------------------------------------------------------*/
  118. void puts(register char *p, byte color){
  119.      while (*p) putc(*p++, color);
  120. }/* puts */
  121.  
  122. /*------------------------------------------------------------------------*
  123.  |           Copy data from far source to far destination                 |
  124.  *------------------------------------------------------------------------*/
  125. void move( char far *src, char far *dest, int amount ){
  126.      register int i;
  127.  
  128.      for (i=0; i<amount; i++) dest[i] = src[i];
  129. }/* move */
  130.  
  131.  
  132. /*------------------------------------------------------------------------*
  133.  |                Copy data from/to the screen                            |
  134.  *------------------------------------------------------------------------*/
  135.  
  136. void scr_move( char far *screen, char far *buffer,
  137.                unsigned amount,
  138.                byte  direction )
  139. {
  140.      if (direction == SAVE)
  141.           move( screen, buffer, amount );
  142.      else move( buffer, screen, amount );
  143. }/* scr_move */
  144.  
  145. /*------------------------------------------------------------------------*
  146.  |                          Exchange two bytes                            |
  147.  *------------------------------------------------------------------------*/
  148. void exchange(byte *a, byte *b){
  149.  
  150.      int temp;
  151.      temp = *a; *a = *b; *b = temp;
  152.  
  153. }/* exchange */
  154.  
  155. /*------------------------------------------------------------------------*
  156.  |                      Order two bytes                                   |
  157.  *------------------------------------------------------------------------*/
  158. void order( byte *a, byte *b){
  159.  
  160.   if (*a > *b)
  161.     exchange( a, b );
  162.  
  163. }/* order */
  164.  
  165. /*------------------------------------------------------------------------*
  166.  |       Save/Restore the box defined by sx, sy, lx, ly  to/from buff     |
  167.  *------------------------------------------------------------------------*/
  168. void box_move( byte sx, byte sy, byte lx, byte ly,
  169.                char far *buff,
  170.                byte direction )
  171. {
  172.   int y, x, i;
  173.  
  174.   order( &sy, &ly );
  175.   x = min(sx, lx);
  176.   i = (abs(lx-sx)+1) << 1;
  177.   scr_move( &screen[(160*sy)+(2*x)], &buff[0], i, direction);
  178.   scr_move( &screen[(160*ly)+(2*x)], &buff[i], i, direction);
  179.   i <<=  1;
  180.   for (y = sy+1; y<ly; y++){
  181.     scr_move( &screen[(160*y)+(2*sx)], &buff[i], 2, direction);
  182.     i += 2;
  183.     scr_move( &screen[(160*y)+(2*lx)], &buff[i], 2, direction);
  184.     i += 2;
  185.   }
  186. }/* box_move */
  187.  
  188. /*------------------------------------------------------------------------*
  189.  |                           Draw box                                     |
  190.  *------------------------------------------------------------------------*/
  191. void draw_box( byte sx, byte sy, byte lx, byte ly ){
  192.      byte x, y;
  193.  
  194.      order( &sy, &ly );
  195.      x = min(sx,lx);
  196.  
  197.      if (sx != lx ){
  198.        gotoxy(x, sy);
  199.        putc(C1, BOX_COLOR); repeat_char(H, abs(lx-sx)-1, BOX_COLOR);
  200.        putc(C2, BOX_COLOR);
  201.        gotoxy(x, ly);
  202.        putc(C3, BOX_COLOR); repeat_char(H, abs(lx-sx)-1, BOX_COLOR);
  203.        putc(C4, BOX_COLOR);
  204.      }
  205.      for (y = sy+1; y <ly; y++){
  206.        gotoxy(sx, y); putc(V, BOX_COLOR); gotoxy(lx, y); putc(V, BOX_COLOR);
  207.      }
  208. }/* draw_box */
  209.  
  210. /*------------------------------------------------------------------------*
  211.  |                         Get box corr.                                  |
  212.  *------------------------------------------------------------------------*/
  213. void get_box_corr(byte *x1, byte *y1, byte *x2, byte *y2){
  214.  
  215.      byte quit, band_on;
  216.      byte x, y;
  217.      int  key;
  218.  
  219.   old_cursor_pos   = 0;
  220.   old_cursor_color = screen[1];
  221.   band_on = FALSE; quit = FALSE;
  222.   x = y = 0;
  223.   do{
  224.     if (!band_on) move_cursor(x,y);
  225.  
  226.     key = getkey();
  227.     if (band_on) box_move( *x1, *y1, x, y, save_buffer, RESTORE );
  228.  
  229.     switch( key ){
  230.        case UP:         y -= (y > MIN_Y);
  231.                         break;
  232.        case DOWN:       y += (y < MAX_Y);
  233.                         break;
  234.        case RIGHT:      x += (x < MAX_X);
  235.                         break;
  236.        case LEFT:       x -= (x > MIN_X);
  237.                         break;
  238.        case HOME:       y -= (y>MIN_Y);
  239.                         x -= (x > MIN_X);
  240.                         break;
  241.        case END:        x -= (x > MIN_X);
  242.                         y += (y < MAX_Y);
  243.                         break;
  244.        case PGDN:       y += (y < MAX_Y);
  245.                         x += (x < MAX_X);
  246.                         break;
  247.        case PGUP:       y -= (y > MIN_Y);
  248.                         x += (x < MAX_X);
  249.                         break;
  250.        case CTRL_RIGHT: x = MAX_X;
  251.                         break;
  252.        case CTRL_LEFT:  x = MIN_X;
  253.                         break;
  254.        case CTRL_HOME:  x = MIN_X; y = MIN_Y;
  255.                         break;
  256.        case CTRL_END:   x = MIN_X; y = MAX_Y;
  257.                         break;
  258.        case CTRL_PGUP:  x = MAX_X; y = MIN_Y;
  259.                         break;
  260.        case CTRL_PGDN:  x = MAX_X; y = MAX_Y;
  261.                         break;
  262.  
  263.        case ENTER:      if (band_on){
  264.                           *x2 = x; *y2 = y;
  265.                           quit = TRUE;
  266.                         }
  267.                         else{
  268.                           band_on = TRUE;
  269.                           *x1 = x; *y1 = y;
  270.                         }
  271.                         break;
  272.       case ESC:         band_on = FALSE;
  273.                         break;
  274.     }
  275.  
  276.     if (band_on){
  277.       box_move( *x1, *y1, x, y, save_buffer, SAVE );
  278.       draw_box( *x1, *y1, x, y);
  279.     }
  280.  
  281.   }while (!quit);
  282.  
  283.  
  284.   if (band_on) box_move( *x1, *y1, x, y, save_buffer, RESTORE );
  285.   screen[old_cursor_pos+1] = old_cursor_color;
  286.   if (*x1 > *x2) exchange(x1, x2);
  287.   if (*y1 > *y2) exchange(y1, y2);
  288.  
  289. }/* get_box_corr */
  290.  
  291. /*------------------------------------------------------------------------*
  292.  |              Yes. return TRUE if y is pressed                          |
  293.  *------------------------------------------------------------------------*/
  294. int yes(char *prompt){
  295.     register key;
  296.     scr_move( screen, save_buffer, (MAX_X+1)*2, SAVE );
  297.     gotoxy(0,0); puts(prompt, ON_COLOR);
  298.     key = getkey(); putc( key, ON_COLOR);
  299.     scr_move( screen, save_buffer, (MAX_X+1)*2, RESTORE );
  300.     return( (key=='y') || (key=='Y') );
  301. }/* yes */
  302.  
  303. /*------------------------------------------------------------------------*
  304.  |           Print a character (using interrupt 0x17)                     |
  305.  *------------------------------------------------------------------------*/
  306. void printc(char c){
  307.      union REGS reg;
  308.  
  309.      reg.h.ah = 0;
  310.      reg.h.al = c;
  311.      reg.x.dx = 0;
  312.      int86( 0x17, ®, ® );
  313. }/* printc */
  314.  
  315. /*------------------------------------------------------------------------*
  316.  |      Print contents of screen (x1, y1) (x2, y2)                        |
  317.  *------------------------------------------------------------------------*/
  318. void print_screen( byte x1, byte y1, byte x2, byte y2 ){
  319.      register x, y;
  320.  
  321.      if (yes("DO YOU WANT TO PRINT THE CURRENT SCREEN ? (Y/N)"))
  322.      {
  323.        gotoxy(0,0);
  324.        for ( y = y1; y <= y2; y++){
  325.          for ( x = x1; x <= x2; x++){
  326.            printc(screen[y*160+x*2]);
  327.          }
  328.          printc(13); printc(10);
  329.        }
  330.      }
  331. }/* print_screen */
  332.  
  333. /*------------------------------------------------------------------------*
  334.  |        Save contents of screen (x1, y1)-(x2, y2)                       |
  335.  *------------------------------------------------------------------------*/
  336. void save_screen( byte x1, byte y1, byte x2, byte y2 ){
  337.     static char *file_name = "OUTPUT.PRN";
  338.     static int  handle = -1;
  339.     static char temp[MAX_X+1];
  340.     register x,y;
  341.  
  342.     if ( yes("DO YOU WANT TO SAVE THE CURRENT SCREEN ? (Y/N)") )
  343.     {
  344.       handle = open(file_name, O_WRONLY|O_APPEND|O_CREAT, S_IREAD|S_IWRITE);
  345.       if (handle > -1){
  346.         for ( y = y1; y <= y2; y++){
  347.           for (x=x1; x<=x2; x++)
  348.             temp[x-x1] = screen[y*160+x*2];
  349.           temp[x-x1]   = 13;
  350.           temp[x-x1+1] = 10;
  351.           _write(handle, temp, x2-x1+3);
  352.         }
  353.         close(handle);
  354.       }
  355.     }
  356. }/* save_screen */
  357.  
  358. /*------------------------------------------------------------------------*
  359.  |                          Main task                                     |
  360.  *------------------------------------------------------------------------*/
  361. void main_task(void){
  362.   byte x1, y1, x2, y2;
  363.  
  364.   old_cursor_pos   = 0;
  365.   old_cursor_color = screen[1];
  366.   get_box_corr(&x1, &y1, &x2, &y2);
  367.   print_screen( x1, y1, x2, y2 );
  368.   save_screen( x1, y1, x2, y2 );
  369.  
  370. }/* main_task */
  371.